package com.jshare.dao.Impl;
// default package

import java.sql.SQLException;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

import com.jshare.dao.DetoolDAO;
import com.jshare.entity.Detool;

/**
 	* A data access object (DAO) providing persistence and search support for Detool entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .Detool
  * @author MyEclipse Persistence Tools 
 */

public class DetoolDaoImpl extends HibernateDaoSupport implements DetoolDAO  {
	     private static final Logger log = LoggerFactory.getLogger(DetoolDaoImpl.class);
		protected void initDao() {
		//do nothing
	}
    
    /* (non-Javadoc)
	 * @see com.jshare.action.DetoolDAO#save(com.jshare.entity.Detool)
	 */
    public void save(Detool transientInstance) {
        log.debug("saving Detool instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	/* (non-Javadoc)
	 * @see com.jshare.action.DetoolDAO#delete(com.jshare.entity.Detool)
	 */
	public void delete(Detool persistentInstance) {
        log.debug("deleting Detool instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    /* (non-Javadoc)
	 * @see com.jshare.action.DetoolDAO#findById(java.lang.Integer)
	 */
    public Detool findById( java.lang.Integer id) {
        log.debug("getting Detool instance with id: " + id);
        try {
            Detool instance = (Detool) getHibernateTemplate()
                    .get(Detool.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    /* (non-Javadoc)
	 * @see com.jshare.action.DetoolDAO#findByExample(com.jshare.entity.Detool)
	 */
    public List findByExample(Detool instance) {
        log.debug("finding Detool instance by example");
        try {
            List results = getHibernateTemplate().findByExample(instance);
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    /* (non-Javadoc)
	 * @see com.jshare.action.DetoolDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding Detool instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from Detool as model where model." 
         						+ propertyName + "= ?";
		 return getHibernateTemplate().find(queryString, value);
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	/* (non-Javadoc)
	 * @see com.jshare.action.DetoolDAO#findByName(java.lang.Object)
	 */
	public List findByName(Object name
	) {
		return findByProperty(NAME, name
		);
	}
	
	/* (non-Javadoc)
	 * @see com.jshare.action.DetoolDAO#findByIntroduce(java.lang.Object)
	 */
	public List findByIntroduce(Object introduce
	) {
		return findByProperty(INTRODUCE, introduce
		);
	}
	
	/* (non-Javadoc)
	 * @see com.jshare.action.DetoolDAO#findByDwUrl(java.lang.Object)
	 */
	public List findByDwUrl(Object dwUrl
	) {
		return findByProperty(DW_URL, dwUrl
		);
	}
	

	/* (non-Javadoc)
	 * @see com.jshare.action.DetoolDAO#findAll()
	 */
	public List findAll() {
		log.debug("finding all Detool instances");
		try {
			String queryString = "from Detool";
		 	return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	
	/* (non-Javadoc)
	 * @see com.jshare.action.DetoolDAO#getListForPage(com.jshare.entity.Detool)
	 */
	public List getListForPage(final int offset, final int length) {
		log.debug("listing Detool instances for page");
		try{
			List list = getHibernateTemplate().executeFind(
					new  HibernateCallback() {
				public Object doInHibernate(Session session) throws HibernateException, SQLException {
					String hql = "from Detool order by downloadcount desc";
					Query query = session.createQuery(hql);
			        query.setFirstResult(offset);
			        query.setMaxResults(length);
			        List list = query.list();
			        return list;
			        }});
		        return list;
		} catch(RuntimeException re) {
			log.error("list for page failed", re);
			throw re;
		}
   }     
	/* (non-Javadoc)
	 * @see com.jshare.action.DetoolDAO#getListByFuzzyName(com.jshare.entity.Detool)
	 */
	public List getListByFuzzyName(String name) {
		log.debug("listing Detool fruzz instance");
        try {
            return getHibernateTemplate()
            .find("from Detool d where d.name like ?", "%"+name+"%");
        } catch (RuntimeException re) {
            log.error("list fuzzy failed", re);
            throw re;
        }
	}
	
    /* (non-Javadoc)
	 * @see com.jshare.action.DetoolDAO#merge(com.jshare.entity.Detool)
	 */
    public Detool merge(Detool detachedInstance) {
        log.debug("merging Detool instance");
        try {
            Detool result = (Detool) getHibernateTemplate()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    /* (non-Javadoc)
	 * @see com.jshare.action.DetoolDAO#attachDirty(com.jshare.entity.Detool)
	 */
    public void attachDirty(Detool instance) {
        log.debug("attaching dirty Detool instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    /* (non-Javadoc)
	 * @see com.jshare.action.DetoolDAO#attachClean(com.jshare.entity.Detool)
	 */
    public void attachClean(Detool instance) {
        log.debug("attaching clean Detool instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

	public static DetoolDAO getFromApplicationContext(ApplicationContext ctx) {
    	return (DetoolDAO) ctx.getBean("DetoolDAO");
	}
}